<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="5" :sm="15">
            <a-form-item label="状态">
              <a-select placeholder="请选择" v-model="queryParam.status" defaultValue="''">
                <a-select-option value="">全部</a-select-option>
                <a-select-option value="0">正常</a-select-option>
                <a-select-option value="1">停用</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" icon="search" @click="this.fetch">查询</a-button>
              <a-button icon="sync" style="margin-left: 8px" @click="handleReset()">重置</a-button>
              <a-button
                v-has="'sys:config:add'"
                style="margin-left: 8px"
                type="primary"
                icon="plus"
                @click="$refs.modal.add()"
                >添加</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-table
      ref="table"
      rowKey="id"
      :pagination="pagination"
      :loading="loading"
      :columns="columns"
      :dataSource="data"
    >
      <span slot="status" slot-scope="text">
        {{ text | statusFilter }}
      </span>

      <span slot="action" slot-scope="text, record">
        <a v-has="'sys:dept:edit'" @click="handleEdit(record)">编辑</a>
        <a-divider type="vertical" />
        <a v-has="'sys:dept:add'" @click="handleAdd(record.id + '')">新增</a>
        <a-divider type="vertical" />
        <a v-has="'sys:dept:remove'" @click="delById(record.id)">删除</a>
      </span>
    </a-table>

    <dept-modal ref="modal" @ok="handleOk" />
  </a-card>
</template>

<script>
import { Table as T } from 'ant-design-vue'
import { getDeptList, delDept } from '@/api/system/sysDept'
import DeptModal from './modules/dept/DeptModal.vue'
import { treeData } from '@/utils/treeutil'
import { checkPermission } from '@/utils/permissions'
export default {
  name: 'TableList',
  components: {
    T,
    DeptModal,
  },
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      form: null,
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '权限名称',
          dataIndex: 'deptName',
        },
        {
          title: '排序',
          dataIndex: 'sort',
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
        },
        {
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
        },
      ],
      data: [],
      pagination: false,
      loading: false,

    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: '停用',
        0: '正常',
      }
      return statusMap[status]
    },
  },
  created() {
    this.fetch()
  },
  methods: {
    handleAdd(parentId) {
      this.$refs.modal.add(parentId)
    },
    handleEdit(record) {
      this.$refs.modal.edit(record)
    },
    handleOk() {
      this.fetch()
    },
    handleReset()  {
      this.queryParam = {
        status: ''
      }
      this.handleOk()
    },
    delById(id) {
      const _this = this
      this.$confirm({
        title: '确定删除吗?',
        onOk() {
           delDept(id).then((res) => {
            if (res.code === 200) {
              _this.$message.success(res.message)
              _this.handleOk()
            } else {
              _this.$message.error(res.message)
            }
            _this.selectedRowKeys = []
          })
        },
        onCancel() {},
      })
    },
    fetch() {
      this.loading = true
      getDeptList(Object.assign(this.queryParam)).then((res) => {
        this.data = treeData(res.result, 'id')
        this.loading = false
        console.log(this.data)
      })
    },
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  },
}
</script>
